package com.jushu.storbox.manager;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.jushu.storbox.task.tqueue.ShareDownloadPriorityTaskScheduler;
import com.jushu.storbox.task.tqueue.ShareDownloadTask;
import com.jushu.storbox.util.LogUtil;

public class ShareDownloadTaskManager extends BaseDownloadManager{
	private static final int POOL_SIZE = 2;
	private static final Set<ShareDownloadTask> downloadSet = new HashSet<ShareDownloadTask>();//all
	private static final ShareDownloadPriorityTaskScheduler<ShareDownloadTask> downloadSequence = new ShareDownloadPriorityTaskScheduler(POOL_SIZE);
	private static final Set<ShareDownloadTask> failedSet = new HashSet<ShareDownloadTask>();//fail
	private static final Set<ShareDownloadTask> runningSet = new HashSet<ShareDownloadTask>();//running
	private static final Set<ShareDownloadTask> waitingSet = new HashSet<ShareDownloadTask>();//waiting
	
	public static Set<ShareDownloadTask> getAllTaskes() {
		return  new HashSet(downloadSet);
	}
	
	public static Set<ShareDownloadTask> getAllRunningTaskes() {
		return  new HashSet(runningSet);
	}
	
	public static Set<ShareDownloadTask> getAllWaitingTaskes() {
		return  new HashSet(waitingSet);
	}
	
	public static void stop() {
		downloadSequence.stop();
	}
	
	public static void start() {
		downloadSequence.start();
	}
	
	public static Set<ShareDownloadTask> getAllTaskes(String folderID) {
		Set<ShareDownloadTask> tasks = new HashSet<ShareDownloadTask>();
		for(Iterator<ShareDownloadTask> itor = downloadSet.iterator(); itor.hasNext();) {
			ShareDownloadTask task = itor.next();
			if (folderID.equalsIgnoreCase(task.getFolderID()))
				tasks.add(task);
		}
		return tasks;
	}
	
	public static boolean addTask(ShareDownloadTask task) {
		synchronized (downloadSet) {
			if (null == task.getId() || "".equalsIgnoreCase(task.getId().trim())) {
				return false;
			}
			if (/*10 >= downloadSet.size() && */downloadSet.add(task) && waitingSet.add(task)) {
				LogUtil.i("ShareDownloadTaskManager", "before add Size is: "+downloaddingSize);
				addDownloaddingSize(task.getFileSize());
				downloadSequence.runTask(task);
				return true;
			} else {
				return false;
			}
		}
	}
	
	public static ShareDownloadTask getTask(String id) {
		ShareDownloadTask task = null;
		for(Iterator<ShareDownloadTask> itor = downloadSet.iterator(); itor.hasNext();) {
			task = itor.next();
			if (id.equalsIgnoreCase(task.getId()))
				return task;
		}
		return null;
	}
	
	public static boolean isRunning(ShareDownloadTask task) {
		synchronized (runningSet) {
			return runningSet.contains(task);
		}
	}
	
	public static boolean contains(ShareDownloadTask task) {
		synchronized (downloadSet) {
			return downloadSet.contains(task);
		}
	}
	
	public static boolean isWaiting(ShareDownloadTask task) {
		synchronized (waitingSet) {
			return waitingSet.contains(task);
		}
	}
	
	public static boolean removeTask(ShareDownloadTask task) {
		downloadSequence.removeTask(task);
		removeRunningTask(task);
		removeWaitingTask(task);
		boolean isRemoveSuccess = false;
		synchronized (downloadSet) {
			isRemoveSuccess = downloadSet.remove(task);
			if(isRemoveSuccess){
				subDownloaddingSize(task.getFileSize());
				LogUtil.i("ShareDownloadTaskManager", "remove task,size is: " + task.getFileSize()
						+"after remove Size  is: "+downloaddingSize);
			}
			return isRemoveSuccess;
		}
	}
	//
	public static boolean addRunningTask(ShareDownloadTask task) {
		synchronized (runningSet) {
			return runningSet.add(task);
		}
	}
	
	public static boolean removeRunningTask(ShareDownloadTask task) {
		synchronized (runningSet) {
			return runningSet.remove(task);
		}
	}
	
	public static boolean addFailTask(ShareDownloadTask task) {
		synchronized (failedSet) {
//			task.deleteSelfFile();
			return failedSet.add(task);
		}
	}
	
	public static boolean removeFailTask(ShareDownloadTask task) {
		synchronized (failedSet) {
			return failedSet.remove(task);
		}
	}
	
	//
	public static boolean addWaitingTask(ShareDownloadTask task) {
		synchronized (waitingSet) {
			return waitingSet.add(task);
		}
	}
	
	public static boolean removeWaitingTask(ShareDownloadTask task) {
		synchronized (waitingSet) {
			return waitingSet.remove(task);
		}
	}
	
}
